<?php

	namespace Phptricks\app\models\users;

/*
	 * To change this license header, choose License Headers in Project Properties.
	 * To change this template file, choose Tools | Templates
	 * and open the template in the editor.
	 * Description of users
	 *
	 * @author Sammy Guergachi <sguergachi at gmail.com>
	 */
	use Phptricks\system\library\models\models as models;
	use Phptricks\system\library\PhptricksExceptions\PhptricksExceptions as Exc;
	use Phptricks\system\core\library\library;

	class users extends models implements library{

		private static $_instance = null;
		private $table_name = 'users';

		public static function &getinstance(){
			if (self::$_instance === null) {
				self::$_instance = new \Phptricks\app\models\users\users();
			}
			return self::$_instance;
		}

		public function get_users($id = 0, $by_group = false, $order_by = 'user_id ASC', $type = 'OR', $limit = 100){
			$array_where = '';
			if ($by_group != false) {
				$by_group = 'user_group_id';
			} else {
				$by_group = 'user_id';
			}
			if (!empty($id)) {
				$array_where = array(
					$by_group => \intval($id),
				);
				if (\is_array($id)) {
					$array_where = array(
						$by_group => \array_filter($id, function(&$k) {
									return $k = \intval($k);
								}),
					);
				}
			}
			try {
				$this->select('', $this->table_name);
				$this->where($array_where, $type);
				$data = $this->execute('', $order_by, '', $limit);
				return $data;
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		public function get_users_by_data($array_where_data = array(), $id = 0, $by_group = false, $order_by = 'user_id ASC', $type = 'OR', $limit = 100){
			$array_where = '';
			if ($by_group != false) {
				$by_group = 'user_group_id';
			} else {
				$by_group = 'user_id';
			}
			if (!empty($id)) {
				$array_where = array(
					$by_group => \intval($id),
				);
				if (\is_array($id)) {
					$array_where = array(
						$by_group => \array_filter($id, function(&$k) {
									return $k = \intval($k);
								}),
					);
				}
			}
			if (\count($array_where_data) > 0) {
				if (\is_array($array_where)) {
					$array_where = \array_merge($array_where, $array_where_data);
				} else {
					$array_where = $array_where_data;
				}
			}
			try {
				$this->select('', $this->table_name);
				$this->where($array_where, $type);
				$data = $this->execute('', $order_by, '', $limit);
				return $data;
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		/**
		 *
		 * @param Integer|Array $id
		 * @return array data of users
		 */
		public function get_users_by_group($array_where){
			try {
				return $this->get_users($array_where, true);
			} catch (\Exception $exc) {
				throw new Exc($exc->getMessage());
			}
		}

		/**
		 * 
		 * @param type $array_data
		 * @return type
		 */
		public function adduser($array_data = array()){
			return $this->insert($this->table_name, $array_data);;
		}

		/**
		 * 
		 * @param array $array_data
		 * @param array|string $where
		 */
		public function updatedata($array_data = array(), $where = ''){
			$this->update($this->table_name, $array_data, $where);
		}

		/**
		 * 
		 * @param array $array_data
		 */
		public function deleteusers($array_data){
			$this->delete($this->table_name, $array_data);
		}

	}
	